/**
 * Created by gongxun on 16/12/12.
 */


var WhmjCardLayer = cc.Layer.extend({

    //存储手牌
    mBatchNode : null,
    //玩家model
    mPlayers : null,
    //最后出牌玩家
    mLastDealCardSeat : null,
    //最后一个牌标记
    mLastPointerSprite : null,
    //滑动出牌的牌
    mSelectMoveSprite : null,
    //胡牌的粒子
    mWinCardParticle : null,
    mWinCardParticle2 : null,
    //创建牌的编号
    mCreatCardIndex : 0,

    ctor : function(){
        this._super();

        //sprite
        cc.spriteFrameCache.addSpriteFrames("res/bigCards.plist", "res/bigCards.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis.plist", "res/emojis.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis_index.plist", "res/emojis_index.png");

        this.mBatchNode = new cc.Node();
        this.addChild(this.mBatchNode);

        //最后一个牌标记
        this.mLastPointerSprite = new cc.Sprite("res/ccs/whmj/table/starpointer.png");
        this.mLastPointerSprite.setVisible(false);
        this.mLastPointerSprite.setScale(0.6);
        this.mLastPointerSprite.setOpacity(225);
        this.mLastPointerSprite.setAnchorPoint(cc.p(0.5, 0));
        this.addChild(this.mLastPointerSprite);


        //胡牌的粒子
        this.mWinCardParticle = new cc.ParticleSystem("res/wincardParticle.plist");
        this.mWinCardParticle.setVisible(false);
        this.addChild(this.mWinCardParticle);

        this.mWinCardParticle2 = new cc.ParticleSystem("res/wincardParticle.plist");
        this.mWinCardParticle2.setVisible(false);
        this.addChild(this.mWinCardParticle2);

        //player
        this.mPlayers = [];
        for (var i = 0; i < 4; i++){
            var palyer = null;
            
            if(i == 0){
                palyer = new WhmjPlayerMyModel();
                palyer.loadTouchListener(this)
            }else{
                palyer = new WhmjPlayerModel();
            }
            palyer.seat = i;
            this.mPlayers.push(palyer);
        }

        //zorder
        this.schedule(function(dt){
            var cards = this.mBatchNode.getChildren();
            var maxZOrder = 0;
            for (var i = 0; i < cards.length; i++){
                var zOrder = cards[i].getCardZ();
                cards[i].setLocalZOrder(zOrder);
                if(zOrder > maxZOrder){
                    maxZOrder = zOrder;
                }
            }
            if(this.mSelectMoveSprite){
                this.mSelectMoveSprite.setLocalZOrder(maxZOrder + 1);
            }
        }, this);
    },

    removeAllCard : function(){
        this.mCreatCardIndex = 0;
        this.mBatchNode.removeAllChildren();
        for (var i = 0; i < 4; i++){
            this.mPlayers[i].reset();
        }

        this.mSelectMoveSprite = null;

        this.setStopWinAnim();
    },

    //返回牌
    getCard : function(){
        var card = new WhmjCard();
        card.mCreatIndex = this.mCreatCardIndex++;
        this.mBatchNode.addChild(card);
        return card;
    },

    //短线重连
    setReconnectStatus : function(tableStatus){
        if (tableStatus.Step == 1){
            //打牌中才处理


            //设置赖子和单杠的牌
            var laiziNum = tableStatus.LZ;
            var singCardNums = tableStatus.SG;
            singCardNums.push(laiziNum);

            mo.whmjCardHelper.setSingCards(singCardNums);
            mo.whmjCardHelper.setLaiziCard(laiziNum);

            mo.whmjTabel.mWhmjAnimLaiziLayer.setLaizi(laiziNum);

            //找到最后打出的牌
            if (tableStatus.LastAction){
                var lastAction = tableStatus.LastAction;
                if (lastAction.code == 8){
                    this.mLastDealCardSeat = mo.whmjTableStatus.getViewForLogic(tableStatus.LastActionSeat - 1, tableStatus.MySeat - 1);
                }
            }

            //放上玩家的牌
            var seatUsers = [tableStatus.SeatUser1, tableStatus.SeatUser2, tableStatus.SeatUser3, tableStatus.SeatUser4];
            for (var seat = 0; seat < 4; seat++){
                var logicSeat = mo.whmjTableStatus.getLogicForView(seat, tableStatus.MySeat - 1);
                var seatUser = seatUsers[logicSeat];

                //设置开口牌
                for (var i = 0; i < seatUser.Groups.length; i++){
                    var cardGroup = seatUser.Groups[i];

                    //计算番数
                    this.mPlayers[seat].updateFanshu(cardGroup.GroupType, cardGroup.Cards);

                    if(cardGroup.GroupType == 7){//如果是单杠
                        var card = this.getWhmjCard();
                        card.setCardNum(cardGroup.Cards[0]);
                        this.mPlayers[seat].addSingCard(card);
                    }else{
                        var cards = [];
                        for (var j = 0; j < cardGroup.Cards.length; j++){
                            var card = this.getWhmjCard();
                            card.setCardNum(cardGroup.Cards[j]);
                            cards.push(card);
                        }

                        if(cardGroup.GroupType == 1){
                            mo.whmjCardHelper.sortCardForNum(cards);
                        }

                        var openCard = mo.whmjCardHelper.getOpenCardStruct(cards, cardGroup.GroupType);
                        this.mPlayers[seat].openCards.push(openCard);
                    }
                }

                //设置手牌
                var handCards = [];
                if (seatUser.HandCards.length == 0){
                    //其他玩家手牌不显示
                    for (var i = 0; i < seatUser.LeftCards; i++){
                        var card = this.getWhmjCard();
                        card.setCardNum(0);
                        handCards.push(card);
                    }
                }else{
                    for(var i = 0; i < seatUser.HandCards.length; i++){
                        var num = seatUser.HandCards[i];
                        var card = this.getWhmjCard();
                        card.setCardNum(num);
                        handCards.push(card);
                    }
                }
                this.mPlayers[seat].inhandCards = handCards;

                //设置打出去的牌
                var dealCards = [];
                for(var i = 0; i < seatUser.PlayCards.length; i++){
                    var num = seatUser.PlayCards[i];
                    var card = this.getWhmjCard();
                    card.setCardNum(num);
                    dealCards.push(card);
                }
                this.mPlayers[seat].dealCards = dealCards;

                this.mPlayers[seat].resetPlayer();
            }
        }
    },


    //添加手牌
    addInhandCard : function(seat, cardNum){
        var card = this.getWhmjCard();
        card.setCardNum(cardNum);
        this.mPlayers[seat].addInhandCard(card);
    },

    //打出手牌
    setDealCard : function(seat, cardNum){
        this.mPlayers[seat].setDealCard(cardNum);
        this.mLastDealCardSeat = seat;

        //标记
        var index = this.mPlayers[seat].dealCards.length - 1;
        var lastCard = this.mPlayers[seat].dealCards[index];
        var p = mo.whmjCardHelper.getDealCardPos(seat, index);
        var size = cc.size(lastCard.getContentSize().width * lastCard.getScaleX(), lastCard.getContentSize().height * lastCard.getScaleY());
        p.x += size.width / 2;
        p.y += size.height / 2;
        this.setLastPointerShow(true, p);
    },

    // setSingCard : function (seat, cardNum) {
    //     this.mPlayers[seat].setSingCard(cardNum);
    // },

    //发牌加动画
    setInhandCards : function(cardNumss){
        for (var i = 0; i < 4; i++){
            var cards = [];
            var cardNums = cardNumss[i];
            for (var j = 0; j < cardNums.length; j++){
                var card = this.getCard();
                card.setCardNum(cardNums[j]);
                cards.push(card);
            }
            this.mPlayers[i].setInhandCards(cards);
        }
    },


    //返回滑动出牌的牌
    getSelectMoveSprite : function(){
        if (this.mSelectMoveSprite == null){
            this.mSelectMoveSprite = this.getWhmjCard();
        }

        this.mSelectMoveSprite.setVisible(false);

        return this.mSelectMoveSprite;
    },

    setLastPointerShow : function(isShow, point){
        if (isShow){
            this.mLastPointerSprite.setVisible(true);
            this.mLastPointerSprite.stopAllActions();
            this.mLastPointerSprite.setPosition(point);
            this.mLastPointerSprite.runAction(
                cc.repeatForever(
                    cc.sequence(
                        cc.moveBy(0.5, cc.p(0, 10)),
                        cc.moveBy(0.5, cc.p(0, -10))
                    )
                )
            );
        }else{
            this.mLastPointerSprite.setVisible(false);
            this.mLastPointerSprite.stopAllActions();
        }
    },

    //开口牌
    setOpenCard : function(seat, cardNums, openType){

        this.setLastPointerShow(false);

        //计算番数
        this.mPlayers[seat].updateFanshu(openType, cardNums);

        var cards = [];
        //从牌桌拿走牌
        if (openType == 1 || openType == 2 || openType == 4){
            //吃，碰和明杠
            //找到牌桌上的牌
            var dealCard = this.mPlayers[this.mLastDealCardSeat].dealCards.pop();
            cards.push(dealCard);

            //剩下牌的号码
            for (var i = 0; i < cardNums.length; i++){
                if (cardNums[i] == dealCard.mCardNum){
                    cardNums.splice(i, 1);
                    break;
                }
            }
        }

        //从手牌中拿走牌
        for (var i = 0; i < cardNums.length; i++){
            var index = mo.whmjCardHelper.findCardForNum(this.mPlayers[seat].inhandCards, cardNums[i]);
            if (index != null){
                cards.push(this.mPlayers[seat].inhandCards[index]);
                this.mPlayers[seat].inhandCards.splice(index, 1);
            }else{
                //手牌中没有找到
                index = this.mPlayers[seat].inhandCards.length - 1;
                var card = this.mPlayers[seat].inhandCards[index];
                card.setCardNum(cardNums[i]);
                cards.push(card);
                this.mPlayers[seat].inhandCards.splice(index, 1);
            }
        }

        if (openType == 6){
            //补杠找到碰
            var openCards = this.mPlayers[seat].openCards;
            for (var i = 0; i < openCards.length; i++){
                var openCard = openCards[i];
                if (openCard.type == 2 && openCard.cards[0].mCardNum == cardNums[0]){
                    openCard.type = 6;
                    openCard.cards.push(cards[0]);
                    break;
                }
            }
        }else if(openType == 7){
            this.mPlayers[seat].addSingCard(cards[0]);
            this.mPlayers[seat].setSingCard();
            return;
        } else{

            if(openType == 1){
                mo.whmjCardHelper.sortCardForNum(cards);
            }

            var openCard = mo.whmjCardHelper.getOpenCardStruct(cards, openType);
            this.mPlayers[seat].openCards.push(openCard);
        }

        this.mPlayers[seat].setOpenCard();
    },

    getWhmjCard : function(){
        var whmjCard = new WhmjCard();
        whmjCard.mCreatIndex = this.mCreatCardIndex++;
        this.mBatchNode.addChild(whmjCard);
        return whmjCard;
    },


    //停止胡牌动画
    setStopWinAnim : function(){
        this.mWinCardParticle.stopAllActions();
        this.mWinCardParticle.setPosition(cc.p(-100, -100));
        this.mWinCardParticle.setVisible(false);

        this.mWinCardParticle2.stopAllActions();
        this.mWinCardParticle2.setPosition(cc.p(-100, -100));
        this.mWinCardParticle2.setVisible(false);
    },

    //胡牌动画
    setWinAnim : function(roundEndInfo){
        this.scheduleOnce(function (dt) {

            //是否有赢家
            var isWin = false;
            //是否是自摸
            var isZimo = false;
            //自摸玩家座位号
            var zimoSeat = -1;
            //不是自摸的话 点炮的座位号
            var dianpaiSeat = -1;

            //翻牌
            for (var seat = 0; seat < 4; seat++){
                var logicSeat = mo.whmjTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
                var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];

                var zimo = false;
                var winCard = seatUser.WinCard;

                //记录胡牌相关
                var isHU = seatUser.WinCard != null && seatUser.WinCard != 0;
                if (isHU){
                    isWin = true;
                    zimo = seatUser.WinCardSource == 0;
                    isZimo = zimo;
                    if (isZimo){
                        zimoSeat = seat;
                    }else{
                        dianpaiSeat = mo.whmjTableStatus.getViewForLogic(seatUser.WinCardSource - 1, roundEndInfo.MySeat - 1);
                    }
                }

                //翻开手牌
                if (seat != 0){
                    var handCardNums = seatUser.HandCards;
                    handCardNums.sort();
                    if (zimo){
                        for (var i = 0; i < handCardNums.length; i++){
                            if (handCardNums[i] == winCard){
                                handCardNums.splice(i, 1);
                                break;
                            }
                        }
                        handCardNums.push(winCard);
                    }

                    if (handCardNums.length == this.mPlayers[seat].inhandCards.length){
                        for (var i = 0; i < this.mPlayers[seat].inhandCards.length; i++){
                            this.mPlayers[seat].inhandCards[i].setCardNum(handCardNums[i]);
                            this.mPlayers[seat].inhandCards[i].setCardKind(seat, 1);

                            var isFromCard = zimo && i == this.mPlayers[seat].inhandCards.length - 1;

                            var openCardsLength = 0;
                            for (var j = 0; j < seatUser.Groups.length; j++){
                                var cardGroupwhmj = seatUser.Groups[j];
                                if(cardGroupwhmj.GroupType == 7){
                                    continue;
                                }
                                openCardsLength++;
                            }

                            var pos = mo.whmjCardHelper.getInhandCardPos(seat, i, openCardsLength, isFromCard);
                            this.mPlayers[seat].inhandCards[i].setPosition(pos);
                        }
                    }
                }
            }

            if (isWin){
                var winCardSprite = null;
                if (isZimo){
                    winCardSprite = this.mPlayers[zimoSeat].inhandCards[this.mPlayers[zimoSeat].inhandCards.length - 1];
                }else{
                    winCardSprite = this.mPlayers[dianpaiSeat].dealCards[this.mPlayers[dianpaiSeat].dealCards.length - 1];
                }
                if (winCardSprite == null){
                    return;
                }
                var rect = winCardSprite.getBoundingBox();
                var speed = 100;

                this.mWinCardParticle.setVisible(true);
                this.mWinCardParticle.stopAllActions();
                this.mWinCardParticle.setPosition(cc.p(cc.rectGetMinX(rect), cc.rectGetMinY(rect)));
                this.mWinCardParticle.runAction(
                    cc.repeatForever(
                        cc.sequence(
                            cc.moveTo(rect.width / speed, cc.p(cc.rectGetMaxX(rect), cc.rectGetMinY(rect))),
                            cc.moveTo(rect.height / speed, cc.p(cc.rectGetMaxX(rect), cc.rectGetMaxY(rect))),
                            cc.moveTo(rect.width / speed, cc.p(cc.rectGetMinX(rect), cc.rectGetMaxY(rect))),
                            cc.moveTo(rect.height / speed, cc.p(cc.rectGetMinX(rect), cc.rectGetMinY(rect)))
                        )
                    )
                );

                this.mWinCardParticle2.setVisible(true);
                this.mWinCardParticle2.stopAllActions();
                this.mWinCardParticle2.setPosition(cc.p(cc.rectGetMaxX(rect), cc.rectGetMaxY(rect)));
                this.mWinCardParticle2.runAction(
                    cc.repeatForever(
                        cc.sequence(
                            cc.moveTo(rect.width / speed, cc.p(cc.rectGetMinX(rect), cc.rectGetMaxY(rect))),
                            cc.moveTo(rect.height / speed, cc.p(cc.rectGetMinX(rect), cc.rectGetMinY(rect))),
                            cc.moveTo(rect.width / speed, cc.p(cc.rectGetMaxX(rect), cc.rectGetMinY(rect))),
                            cc.moveTo(rect.height / speed, cc.p(cc.rectGetMaxX(rect), cc.rectGetMaxY(rect)))
                        )
                    )
                );
            }
        }, 0.5);
    },
});